<template>
    <!-- 商品详情-弹出层-收货地址组件 -->
    <view class="address-panel">
      <!-- 关闭按钮 -->
      <text class="close icon-close" @tap="close_btn"></text>
      <!-- 标题 -->
      <view class="title">配送至</view>
      <!-- 内容 -->
      <view class="content">
        <!-- 地址item项 -->
        <view class="item" v-for="item,index in userAddressList" :key="index" @tap="updateAddress(item)">
          <view class="user">{{item.receiver +" "+ item.contact}}</view>
          <view class="address">{{item.fullLocation +'-'+ item.address}}</view>
          <text class="icon" :class="item.isDefault == 1?'icon-checked':'icon-ring'"></text>
        </view>
      </view>
      <view class="footer">
        <view class="button primary" @tap="addAddress"> 新建地址 </view>
        <view v-if="false" class="button primary">确定</view>
      </view>
    </view>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { onShow, onLoad } from "@dcloudio/uni-app";
//引入请求函数
import { reqUserAddress, reqUpdateUserAddress } from "@/services/address";
//引入类型约束
import type { addressResponse } from "@/types/address";

//自定义事件
const emit = defineEmits<{
  (event:'close'):void,
  (event:'sendAddressInfo'):void
}>()

//收货地址列表数据
let userAddressList = ref<addressResponse[]>([]);

//vue生命周期
onMounted(()=>{
  //获取地址
  getUserAddressList();
})

//获取用户的收货地址
const getUserAddressList = async () => {
  let res = await reqUserAddress();
  userAddressList.value = res.result;
  console.log('收货地址api',res);
}
//地址项的点击事件，修改默认地址
const updateAddress = async (address:any) => {
  userAddressList.value.forEach(item=>{
    item.isDefault = 0;
  })
  address.isDefault == 0 ? address.isDefault = 1 : address.isDefault = 0 ;
  //调用修改地址api
  let res = await reqUpdateUserAddress(address.id,{isDefault:address.isDefault});
  uni.showToast({icon:'none',title:"默认地址修改成功!"});
  console.log('更新地址api',res);
}
//新建地址按钮:跳转到新建地址页
const addAddress = () => {
  uni.navigateTo({url:'/subPages/addressForm/addressForm'});
}

//功能函数-->获取默认地址的详细地址，返回string
const getAddressStr = ():string => {
  for(let i=0; i<userAddressList.value.length; i++){
    if(userAddressList.value[i].isDefault == 1){
      return userAddressList.value[i].fullLocation +' '+ userAddressList.value[i].address;
    }
  }
}
//关闭popup弹窗的按钮
const close_btn = () => {
  emit('close');  //关闭popup弹窗
  emit('sendAddressInfo',getAddressStr());
}

</script>

<style lang="scss">
  .address-panel {
    padding: 0 30rpx;
    border-radius: 10rpx 10rpx 0 0;
    position: relative;
    background-color: #fff;
  }
  
  .title {
    line-height: 1;
    padding: 40rpx 0;
    text-align: center;
    font-size: 32rpx;
    font-weight: normal;
    border-bottom: 1rpx solid #ddd;
    color: #444;
  }
  
  .close {
    position: absolute;
    right: 24rpx;
    top: 24rpx;
  }
  
  .content {
    min-height: 300rpx;
    max-height: 540rpx;
    overflow: auto;
    padding: 20rpx;
    .item {
      padding: 30rpx 50rpx 30rpx 60rpx;
      background-size: 40rpx;
      background-repeat: no-repeat;
      background-position: 0 center;
      background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
      position: relative;
    }
    .icon {
      color: #999;
      font-size: 40rpx;
      transform: translateY(-50%);
      position: absolute;
      top: 50%;
      right: 0;
    }
    .icon-checked {
      color: #27ba9b;
    }
    .icon-ring {
      color: #444;
    }
    .user {
      font-size: 28rpx;
      color: #444;
      font-weight: 500;
    }
    .address {
      font-size: 26rpx;
      color: #666;
    }
  }
  
  .footer {
    display: flex;
    justify-content: space-between;
    padding: 20rpx 0 40rpx;
    font-size: 28rpx;
    color: #444;
  
    .button {
      flex: 1;
      height: 72rpx;
      text-align: center;
      line-height: 72rpx;
      margin: 0 20rpx;
      color: #fff;
      border-radius: 72rpx;
    }
  
    .primary {
      color: #fff;
      background-color: #27ba9b;
    }
  
    .secondary {
      background-color: #ffa868;
    }
  }
  </style>